﻿using System;
//using System.Net;
using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Documents;
//using System.Windows.Ink;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Animation;
//using System.Windows.Shapes;
using Microsoft.Phone.Controls.Maps;
using System.Collections.Generic;
using System.Device.Location;

namespace SimpleRadar
{
    public static class MapPolygonEx
    {
        public static void DrawCircler(this MapPolygon obj, GeoCoordinate location, double Zoom)
        {
            obj.Locations = new LocationCollection();

            var earthRadius = 6371;
            var lat = location.Latitude * Math.PI / 180.0; //radians 
            var lon = location.Longitude * Math.PI / 180.0; //radians 
            var d = location.HorizontalAccuracy / Zoom / earthRadius; // d = angular distance covered on earths surface 

            for (int x = 0; x <= 360; x++)
            {
                var brng = x * Math.PI / 180.0; //radians 
                var latRadians = Math.Asin(Math.Sin(lat) * Math.Cos(d) + Math.Cos(lat) * Math.Sin(d) * Math.Cos(brng));
                var lngRadians = lon + Math.Atan2(Math.Sin(brng) * Math.Sin(d) * Math.Cos(lat), Math.Cos(d) - Math.Sin(lat) * Math.Sin(latRadians));
                var pt = new GeoCoordinate(180.0 * latRadians / Math.PI, 180.0 * lngRadians / Math.PI);
                obj.Locations.Add(pt);
            }
        }
        public static bool Contains(this MapPolygon polyPoints, GeoCoordinate point)
        {
            if (polyPoints.Locations == null) return false;

            if (polyPoints.Locations.Count < 3)
            {
                return false;
            }

            bool inside = false;
            GeoCoordinate p1, p2;

            //iterate each side of the polygon
            GeoCoordinate oldPoint = polyPoints.Locations[polyPoints.Locations.Count - 1];

            foreach (GeoCoordinate newPoint in polyPoints.Locations)
            {
                //order points so p1.lat <= p2.lat;
                if (newPoint.Latitude > oldPoint.Latitude)
                {
                    p1 = oldPoint;
                    p2 = newPoint;
                }
                else
                {
                    p1 = newPoint;
                    p2 = oldPoint;
                }

                //test if the line is crossed and if so invert the inside flag.
                if ((newPoint.Latitude < point.Latitude) == (point.Latitude <= oldPoint.Latitude)
                    && (point.Longitude - p1.Longitude) * (p2.Latitude - p1.Latitude)
                     < (p2.Longitude - p1.Longitude) * (point.Latitude - p1.Latitude))
                {
                    inside = !inside;
                }

                oldPoint = newPoint;
            }

            return inside;
        }
        public static bool CheckInPoint(this LocationCollection polyPoints, GeoCoordinate point)
        {
            if (polyPoints.Count < 3)
            {
                return false;
            }

            bool inside = false;
            GeoCoordinate p1, p2;

            //iterate each side of the polygon
            GeoCoordinate oldPoint = polyPoints[polyPoints.Count - 1];

            foreach (GeoCoordinate newPoint in polyPoints)
            {
                //order points so p1.lat <= p2.lat;
                if (newPoint.Latitude > oldPoint.Latitude)
                {
                    p1 = oldPoint;
                    p2 = newPoint;
                }
                else
                {
                    p1 = newPoint;
                    p2 = oldPoint;
                }

                //test if the line is crossed and if so invert the inside flag.
                if ((newPoint.Latitude < point.Latitude) == (point.Latitude <= oldPoint.Latitude)
                    && (point.Longitude - p1.Longitude) * (p2.Latitude - p1.Latitude)
                     < (p2.Longitude - p1.Longitude) * (point.Latitude - p1.Latitude))
                {
                    inside = !inside;
                }

                oldPoint = newPoint;
            }

            return inside;
        }
    }
}
